﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Finance.EntityFrameworkCore;
using mozhi.smarterp.Finance.Treasuries.Payments.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.Finance.Treasuries.Payments
{
    public class PaymentRequisitionLinkRepository : EfCoreRepository<FinanceDbContext, PaymentRequisitionLink, Guid>, IPaymentRequisitionLinkRepository
    {
        public PaymentRequisitionLinkRepository(IDbContextProvider<FinanceDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public async Task<long> GetCountAsync(Guid? requisitionId, Guid? paymentId, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var count = await dbContext.Set<PaymentRequisitionLink>().AsNoTracking()
                .WhereIf(requisitionId.HasValue, x => x.PaymentRequisitionId == requisitionId)
                .WhereIf(requisitionId.HasValue, x => x.PaymentId == paymentId)
                .CountAsync();

            return count;
        }

        public async Task<List<PaymentRequisitionLink>> GetListAsync(Guid? requisitionId, Guid? paymentId, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var links = await dbContext.Set<PaymentRequisitionLink>().AsNoTracking()
                .WhereIf(requisitionId.HasValue, x => x.PaymentRequisitionId == requisitionId)
                .WhereIf(requisitionId.HasValue, x => x.PaymentId == paymentId)
                .ToListAsync();

            return links;
        }
    }
}
